package com.love_weinuo.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.lzy.okgo.callback.StringCallback;
import com.love_weinuo.R;
import com.love_weinuo.bean.AddCodeCallBackBean;
import com.love_weinuo.bean.MyOrderCallbackBean;
import com.love_weinuo.bean.MyOrderChangeCallbackBean;
import com.love_weinuo.databinding.ActivityMyOrderBinding;
import com.love_weinuo.ui.BaseActivity;
import com.love_weinuo.ui.adapter.IndexFirstAdapter;
import com.love_weinuo.ui.adapter.OnItemClickListener;
import com.love_weinuo.util.GsonUtil;
import com.love_weinuo.util.HTTPAPI;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.SpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by SensYang on 2019/02/28 15:00
 */

public class MyOrderActivity extends BaseActivity {

    ActivityMyOrderBinding binding;
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> strs = new ArrayList<>();
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> pindanList = new ArrayList<>();
    IndexFirstAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_order);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        add(this);
        setBack();
        setTitleText(getResources().getString(R.string.my_order));
        binding.recycler.setLayoutManager(new LinearLayoutManager(this));
        adapter = new IndexFirstAdapter(this, strs);
        binding.recycler.setAdapter(adapter);
        adapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                getPindanDesc(strs.get(position).getId() + "");
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        getAllMyOrder();
    }

    String userId = (String) SpUtils.get("module_user_id", "");

    public void getAllMyOrder() {
        HTTPAPI.getInstance().getAllMyOrder(userId, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("获取订单成功" + s);
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    int errorCode = jsonObject.getInt("errcode");
                    if (errorCode == 1) {
                        binding.recycler.setVisibility(View.GONE);
                        binding.ilModuleNothing.setVisibility(View.VISIBLE);
                        showInCommonDialog(jsonObject.getString("msg"));
                        return;
                    }
                    MyOrderChangeCallbackBean bean = GsonUtil.GsonToBean(s, MyOrderChangeCallbackBean.class);
                    switch (bean.getErrcode()) {
                        case 0:
                            pindanList.clear();
                            pindanList.addAll(bean.getResult().getRes());
                            if (pindanList.size() == 0) {
                                if (strs.size() != 0) {
                                    showInCommonDialog("暂无更多数据");
                                    return;
                                } else {
                                    binding.recycler.setVisibility(View.GONE);
                                    binding.ilModuleNothing.setVisibility(View.VISIBLE);
                                }
                            } else {
                                binding.recycler.setVisibility(View.VISIBLE);
                                binding.ilModuleNothing.setVisibility(View.GONE);
                                strs.clear();
                                strs.addAll(pindanList);
                                adapter.notifyDataSetChanged();
                            }
                            break;
                        default:
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    LogUtil.e("json解析错误" + e.getMessage());
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("获取订单失败" + e.getMessage());
            }
        });
    }

    public void getPindanDesc(final String pindanId) {
        HTTPAPI.getInstance().getPidanDesc(userId, pindanId, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("拼单详情获取成功" + s);
                addCode(s, pindanId);
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("拼单详情获取失败" + e.getMessage());
            }
        });
    }


    public void addCode(final String answer, String pindanId) {
        HTTPAPI.getInstance().addCode(userId, pindanId, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("邀请码获取成功" + s);
                AddCodeCallBackBean bean = GsonUtil.GsonToBean(s, AddCodeCallBackBean.class);
                String inviteCode = bean.getResult().getCode();
                SpUtils.put("module_broadcast_groupid", inviteCode);
                startActivity(new Intent(MyOrderActivity.this, PindanZxingDescActivity.class)
                        .putExtra("module_result", answer));
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("邀请码获取失败" + e.getMessage());
            }
        });
    }
}
